package com.zyzd.retrofit.api;


import com.zyzd.commontools.utils.LogUtils;

import org.reactivestreams.Subscriber;

import retrofit2.HttpException;

/**
 * Created by 李宗源 on 2017/4/25.
 */

public abstract class ApiCallback<T> implements Subscriber<T> {


    public abstract void onFailure(String msg);

    @Override
    public void onError(Throwable e) {
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            int code = httpException.code();
            String msg = httpException.getMessage();
            LogUtils.d("ApiCallback>>onError--> code=" + code);
            LogUtils.d("zyzd", "ApiCallback>>onError--> msg" + msg);
            if (code == 504) {
                msg = "网络不给力";
            }
            if (code == 502 || code == 404) {
                msg = "服务器异常，请稍后再试";
            }
            onFailure(msg);
        } else {
            onFailure(e.getMessage());
        }
    }

}
